package org.cheetah.spring.xml;

import java.util.List;

import org.cheetah.spring.xml.factory.ServiceFactoryBean;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;

public class ServiceDefinitionParser extends AbstractSingleBeanDefinitionParser {
	@Override
	protected Class<?> getBeanClass(Element element) {
		return ServiceFactoryBean.class;
	}

	@Override
	protected void doParse(Element element, ParserContext context,
			BeanDefinitionBuilder bean) {
		List<?> activities = context.getDelegate().parseListElement(element,
				bean.getRawBeanDefinition());

		String predefinedVars = element.getAttribute("var-ref");
		if (StringUtils.hasText(predefinedVars)) {
			bean.addPropertyReference("vars", predefinedVars);
		}

		bean.addPropertyValue("activities", activities);
	}
}
